package com.cjl.multi_thread.aqs;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class DebugAQSDemo {

    public static int count = 0;

    public static Lock lock = new ReentrantLock();

    public static void main(String[] args) {
//
//
//        try {
//            lock.tryLock();
//        }finally {
//            lock.unlock();
//        }

        for (int i = 0; i < 10; i++) {
            new Thread(new WriteThread()).start();
            new Thread(new ReadThread()).start();
            new Thread(new ReadThread()).start();
        }

    }

    static class Change implements Runnable{

        private int j ;

        public Change(int i) {
            this.j = i;
        }

        @Override
        public void run() {

            Random random = new Random();
            try {
                Thread.sleep(random.nextInt(1000));
                for (int i = 0; i < 10; i++) {
                    count++;
//                    if (j % 2 == 0){
//                        count++;
//                    }else {
//                        count--;
//                    }
                }
                System.out.println("Change.run count "+count);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    static class ReadThread implements Runnable{

        @Override
        public void run() {
            try {
                lock.tryLock();
                System.out.println("WriteThread.run  取 100");
                count -= 100;
                if (count < 0){
                    System.out.println("ReadThread.run 剩余 "+count);
                }

                try {
                    Thread.sleep((new Random()).nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }finally {
                lock.unlock();
            }


        }
    }


    static class WriteThread implements Runnable{

        @Override
        public void run() {

            try {
                lock.tryLock();
                System.out.println("WriteThread.run  存 200");
                count += 200;
                try {
                    Thread.sleep((new Random()).nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }finally {
                lock.unlock();
            }


        }
    }

    static class User{
        private String name;

        public User(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
}
